<?php $__env->startSection('content'); ?>
   <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品列表</strong> /
                <small>Product List</small>
            </div>
        </div>
       <br>
<?php echo $__env->make('admin.layouts._flash', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

        <div class="am-g" style="height: 37px;">
            <div class="am-u-sm-12 am-u-md-3">
                <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                        <a type="button" class="am-btn am-btn-default" href="<?php echo e(route('shop.product.create')); ?>">
                            <span class="am-icon-plus"></span> 新增
                        </a>
                        <button type="button" class="am-btn am-btn-default" id="destroy_checked">
                            <span class="am-icon-trash-o"></span> 删除
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-12">
                <form class="am-form-inline" role="form" method="get">

                    <div class="am-form-group">
                        <input type="text" name="name" class="am-form-field am-input-sm" placeholder="商品名" value="<?php echo e(Request::input('name')); ?>">
                    </div>

                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm', maxHeight: 360, searchBox: 1}" name="category_id" style="display: none;">
                            <optgroup label="请选择">
                                <option value="-1">所有分类</option>
                            </optgroup>
                        <?php foreach($categories as $category): ?>
                            <optgroup label="<?php echo e($category->name); ?>">
                                <?php foreach($category->children as $c): ?>
                                <option value="<?php echo e($c->id); ?>" <?php if($c->id==Request::input('category_id')): ?> selected <?php endif; ?>>
                                    <?php echo e($c->name); ?>

                                </option>
                               <?php endforeach; ?>
                            </optgroup>
                        <?php endforeach; ?>
                        </select>

                    </div>

                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm', maxHeight: 360, searchBox: 1}" name="brand_id" id="" style="display: none;">
                            <option value="-1">所有品牌</option>
                           <?php foreach($brands as $brand): ?>
                            <option value="<?php echo e($brand->id); ?>" <?php if($brand->id==Request::input('brand_id')): ?> selected <?php endif; ?>>
                                <?php echo e($brand->name); ?>

                            </option>
                        <?php endforeach; ?>
                        </select>

                    </div>

                    <div class="am-form-group">
                        <div class="am-btn-group" data-am-button="">
                            <label class="am-btn am-btn-default am-btn-sm am-radius <?php if(Request::input('is_top')==1): ?> am-active <?php endif; ?> ">
                                <input type="checkbox" name="is_top" value="1" <?php if(Request::input('is_top')==1): ?> checked <?php endif; ?>> 置顶
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm am-radius <?php if(Request::input('is_recommend')==1): ?> am-active <?php endif; ?>">
                                <input type="checkbox" name="is_recommend" value="1" <?php if(Request::input('is_recommend')==1): ?> checked <?php endif; ?>> 推荐
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm am-radius <?php if(Request::input('is_hot')==1): ?> am-active <?php endif; ?>">
                                <input type="checkbox" name="is_hot" value="1" <?php if(Request::input('is_hot')==1): ?> checked <?php endif; ?>> 热销
                            </label>
                            <label class="am-btn am-btn-default am-btn-sm am-radius <?php if(Request::input('is_new')==1): ?> am-active <?php endif; ?>">
                                <input type="checkbox" name="is_new" value="1" <?php if(Request::input('is_new')==1): ?> checked <?php endif; ?>> 新品
                            </label>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm'}" name="is_onsale" id="" style="display: none;">
                            <option value="-1" <?php if(Request::input('is_onsale')=='-1'): ?> selected <?php endif; ?> >上架状态</option>
                            <option value="1" <?php if(Request::input('is_onsale')=='1'): ?> selected <?php endif; ?>>上架</option>
                            <option value="0" <?php if(Request::input('is_onsale')=='0'): ?> selected <?php endif; ?>>下架</option>
                        </select>
                    </div>

                    <div class="am-form-group">
                        <input type="text" id="created_at" placeholder="选择时间日期" name="created_at" value="<?php echo e(Request::input('created_at')); ?>" class="am-form-field am-input-sm">
                    </div>

                    <button type="submit" class="am-btn am-btn-default am-btn-sm">查询</button>
                </form>
            </div>
        </div>


        <div class="am-g">
            <div class="am-u-sm-12">
                <form class="am-form" id="form">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th class="table-check"><input type="checkbox" id="checked"></th>
                            <th class="table-id">ID</th>
                            <th class="table-thumb">缩略图</th>
                            <th class="table-title">商品名称</th>
                            <th class="table-category">所属分类</th>
                            <th class="table-brand">品牌</th>
                            <th class="table-price">单价</th>
                            <th class="am-hide-sm-only">上架</th>
                            <th class="am-hide-sm-only">置顶</th>
                            <th class="am-hide-sm-only">推荐</th>
                            <th class="am-hide-sm-only">热销</th>
                            <th class="am-hide-sm-only">新品</th>
                            <th class="am-hide-sm-only" style="width:10%">库存</th>
                            <th class="table-date am-hide-sm-only">上架日期</th>

                            <th class="table-set">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach($products as $product): ?>
                        <tr data-id="<?php echo e($product->id); ?>">
                            <td><input type="checkbox" value="<?php echo e($product->id); ?>" class="checked_id" name="checked_id[]"></td>
                            <td><?php echo e($product->id); ?></td>
                            <td>
                               <?php if($product->thumb): ?> <img src="<?php echo e($product->thumb); ?>" alt="" class="thumb" style="height: 60px"> <?php endif; ?>   </td>
                            <td>
                                <?php echo e($product->name); ?>

                            </td>

                            <td><?php echo e(isset($product->category->name) ? $product->category->name : ''); ?></td>

                            <td><?php echo e(isset($product->brand->name) ? $product->brand->name : ''); ?></td>

                            <td>
                                <?php echo e($product->price); ?>

                            </td>
                            <?php foreach(array('is_onsale', 'is_top', 'is_recommend', 'is_hot', 'is_new') as $attr): ?>
                            <td class="am-hide-sm-only">
                                <?php echo is_show($attr,$product); ?>

                            </td>

                            <?php endforeach; ?>

                            <td class="am-hide-sm-only">
                                <input type="text" name="stock" class="am-input-sm" value="<?php echo e($product->stock==-1?'无限':$product->stock); ?>">
                            </td>

                            <td class="am-hide-sm-only">
                                <?php echo e($product->created_at); ?>

                            </td>

                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <a class="am-btn am-btn-default am-btn-xs am-text-secondary" href="<?php echo e(route('shop.product.edit',[$product->id])); ?>">
                                            <span class="am-icon-pencil-square-o"></span> 编辑
                                        </a>

                                        <a class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only" href="<?php echo e(route('shop.product.destroy',[$product->id])); ?>"
                                           data-method="delete"   data-token="<?php echo e(csrf_token()); ?>"  data-confirm="确认删除吗?">
                                            <span class="am-icon-trash-o"></span> 删除
                                        </a>
                                    </div>
                                </div>
                            </td>
                        </tr>
                      <?php endforeach; ?>
                        </tbody>
                    </table>

                    <div class="am-cf" style="float: right">

                        <?php echo $products->appends(Request::all())->links(); ?>


                    </div>
                    <hr />
                    <p>共<?php echo e($products->total()); ?>条记录</p>


                    <hr>
                </form>
            </div>

        </div>

    </div>


    <?php $__env->stopSection(); ?>

<?php $__env->startSection('js'); ?>
    <script src="/assets/vendor/daterangepicker/moment.js"></script>
    <script src="/assets/vendor/daterangepicker/daterangepicker.js"></script>
    <script src="/assets/vendor/moment/locale/zh-cn.js"></script>
    <script src="/assets/js/daterange_config.js"></script>



    <script>

        $(function(){

            //上架	置顶	推荐	热销	新品
            $('.is_something').click(function(){
                var _this=$(this);
                var attr=$(this).attr('data-attr');

                var id=$(this).parents('tr').attr('data-id');
                $.ajax({
                    url:'/shop/product/is_something',
                    data:{id:id,attr:attr},
                    type:'patch',
                    success:function(data){
                        console.log(data);
                        _this.toggleClass('am-icon-close am-icon-check');
                    }
                })
            })



            //库存
            $("input[name='stock']").change(function () {
                var data = {
                    stock: $(this).val(),
                    id: $(this).parents("tr").attr('data-id')
                }

                $.ajax({
                    url:'/shop/product/stock',
                    type: 'patch',
                    data: data,
                });
            })



            //多选
            $('#checked').click(function(){
                $('.checked_id').prop('checked',this.checked);
            })

            //多选删除 用ajax实现  动态绑定事件
            $(document).on('click','#destroy_checked',function(){
                var length=$('.checked_id:checked').length;
                if (length == 0) {
                    alert("您必须至少选中一条!");
                    return false;
                }

//                关键是获得要删除的ids数组
                var ids='';
                $(".checked_id:checked").each(function(){
                    ids+=$(this).val()+' ';
                })
                console.log(ids);

               $.ajax({
                   url:'/shop/product/destroy_checked',
                   type:'delete',
                   data:{ids:ids},
                   success:function(data){
                       console.log(data);
                       location.href = location.href;
                   }
               })
            })

        })

    </script>
    <?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>